//
//  ShoppingViewController.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/12/2.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit
import WebKit


class ShoppingViewController: WKBaseViewController {

    
//    var urlStr : String = ""
    
//    
//    override func viewWillAppear(_ animated: Bool) {
//        super.viewWillAppear(animated)
//        
//        webView.load(URLRequest.init(url: URL.init(string: shooppingCarURL)!))
//    }

    override func viewDidLoad() {
        
        super.viewDidLoad()
        self.navigationItem.title = "购物车"
        
        print("购物车最后一个链接",shoppingCarArray.lastObject as Any)
        
        //背景色--白色
        view.backgroundColor = UIColor.white

            if shoppingCarArray.count > 1 {
                
                
                let urlStr = ((shoppingCarArray.lastObject) as! String)
                
                webView.load(URLRequest.init(url: URL.init(string: urlStr)!))
            } else  {
                DispatchQueue.main.async {
                    
                    let urlStr = (shooppingCarURL) + ("?devtype=1&token=") + self.token
            
                    
                    
                    if urlStr.contains(comStrURL) {
                        let url = URL.init(string: urlStr)
                        
                        TestNet.shared.getNetStatus(comfun: { (result) in
                            if result.rawValue == "worked" {
                                print("worked")
                                
                                DispatchQueue.main.async {
                                    
                                    self.urlRequestCache = NSURLRequest(url: url!, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 10)
                                    self.webView.load(self.urlRequestCache as URLRequest)
                                }
                                
                            } else {
                                
                                DispatchQueue.main.async {
                                    
                                    print("本地")
                                    print(self.webView.bounds)
                                    
                                    self.urlRequestCache = NSURLRequest.init(url: url!, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 0)
                                    self.webView.load(self.urlRequestCache as URLRequest)
                                    
                                    
                                }
                                
                            }
                            
                        })
                    } else {
                        CustomAlertView.shared.alertWithTitle(strTitle: "请下拉刷新")

                    }
                    
                    
                    
                }
                
//            }
        }
    }
    

    
    /**
     *  JS 调用 swift 时 webview 会调用此方法
     *
     *  @param userContentController  webview中配置的userContentController 信息
     *  @param message                JS执行传递的消息
     */

    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        
        textView.text = "\((#file as NSString).lastPathComponent):(\(#line))" + url
        
        url = (navigationAction.request.url?.absoluteString)!
        
        print("orderPay",url)
        
        print(shoppingCarArray.count)
        
        
        if navigationAction.navigationType == WKNavigationType.linkActivated {
            
            //如果为本身或者本身携带#的url进行自身刷新。
            if url == shooppingCarURL {
                
                self.webView.load(URLRequest.init(url: URL.init(string: url)!))
                
                shoppingCarArray.removeAllObjects()
                
            } else {
                print(shoppingCarArray)
                
                //取出本地token，进行拼接
                if (localSave.object(forKey: userToken) != nil) {
                    let token = localSave.object(forKey: userToken) as! String
                    
                    print(url)
                    
                    
                    if self.url.contains("?") {
                        self.url = self.url + ("&devtype=1&token=") + (token)
                    } else {
                        self.url = self.url + ("?devtype=1&token=") + (token)
                        
                        print("self,yrk",self.url)
                    }
                }
                
                shoppingCarArray.add(url)
                aaa(str: shoppingCarArray.lastObject as! String)
            }
            
            decisionHandler(.cancel)
            
        } else {
            decisionHandler(.allow)
        }
    }
    //url---
    func aaa(str : String) -> Void {
        
        let vvv = ShoppingReplaceView()
        vvv.url = str
        self.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vvv, animated: true)
        self.hidesBottomBarWhenPushed = false
        
    }


}

